//
//  TVController.m
//  GKBHouse
//
//  Created by  on 12-7-1.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "TVController.h"
#import "GKBNavigationBar.h"
#import "DeviceView.h"
#import "LoginController1.h"

@interface TVController ()

@property (retain, nonatomic)IBOutlet UIButton *btn1;
@property (retain, nonatomic)IBOutlet UIButton *btn2;
@property (retain, nonatomic)IBOutlet UIButton *btn3;
@property (retain, nonatomic)IBOutlet UIButton *btn4;
@property (retain, nonatomic)IBOutlet UIButton *btn5;
@property (retain, nonatomic)IBOutlet UIButton *btn6;
@property (retain, nonatomic)IBOutlet UIButton *btn7;
@property (retain, nonatomic)IBOutlet UIButton *btn8;
@property (retain, nonatomic)IBOutlet UIButton *btn9;
@property (retain, nonatomic)IBOutlet UIButton *btn0;

@property (retain, nonatomic)IBOutlet UIButton *btn__;
@property (retain, nonatomic)IBOutlet UIButton *btnTVAV;
@property (retain, nonatomic)IBOutlet UIButton *btnMenu;
@property (retain, nonatomic)IBOutlet UIButton *btnReturn;
@property (retain, nonatomic)IBOutlet UIButton *btnVioce;
@property (retain, nonatomic)IBOutlet UIButton *btnOnOff;

@property (retain, nonatomic)IBOutlet UIButton *btnOk;
@property (retain, nonatomic)IBOutlet UIButton *btnUp;
@property (retain, nonatomic)IBOutlet UIButton *btnDown;
@property (retain, nonatomic)IBOutlet UIButton *btnLeft;
@property (retain, nonatomic)IBOutlet UIButton *btnRight;

@end

@implementation TVController
@synthesize equipment;
@synthesize ctrOrder;

- (void)dealloc
{
    self.equipment = nil;
    self.ctrOrder = nil;
    
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (id)initWithEquipment:(NewEquipment *)equ
{
    self = [super init];
    if (self) {
        // Custom initialization
        self.equipment = equ;
    }
    return self;
}
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];
    [self.view setFrame:CGRectMake(0, 0, DEVICE_HEIGHT, DEVICE_WIDTH)];
    
    [self initTopBar];
    
    
    [OnOff addTarget:self action:@selector(ctlDevice) forControlEvents:UIControlEventTouchUpInside];
    
    if([self.equipment.state isEqualToString: kStateON])
    {
        [DeviceView setDeivceOnOff:self.equipment button:OnOff State:YES];
    }
    else
    {
        [DeviceView setDeivceOnOff:self.equipment button:OnOff State:NO];
    }
    
    if (IOS7_OR_LATER) {
        NSArray *array = @[self.btn0, self.btn1, self.btn2, self.btn3, self.btn4, self.btn5, self.btn6, self.btn7, self.btn8, self.btn9, self.btn__, self.btnMenu, self.btnOnOff, self.btnReturn, self.btnTVAV, self.btnVioce, self.btnOk, self.btnUp, self.btnDown, self.btnLeft, self.btnRight];
        
        for (UIButton *btn in array) {
            CGRect frame = btn.frame;
            
            frame.origin.y = frame.origin.y + 5;
            [btn setFrame:frame];
            
        }
    }
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

//- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
//{
//    // Return YES for supported orientations
//    return (interfaceOrientation == UIInterfaceOrientationPortrait);
//}
- (void)initTopBar
{    
    GKBNavigationBar *topBar = [[GKBNavigationBar alloc]init];
    [topBar setTarget:self returnAction:@selector(closeView) withTitle:self.equipment.name];
    [self.view addSubview:topBar];    
    [topBar release];
    
}
- (void)closeView
{
    [GKBMainController refreshDevices];
    [self.view removeFromSuperview];
    [GKBMainController postStartRefresh];
}
#pragma mark - order
- (IBAction)C1
{
    [self ctrTV:@"1"];
}
- (IBAction)C2
{
    [self ctrTV:@"2"];
}
- (IBAction)C3
{
    [self ctrTV:@"3"];
}
- (IBAction)C4
{
    [self ctrTV:@"4"];
}
- (IBAction)C5
{
    [self ctrTV:@"5"];
}
- (IBAction)C6
{
    [self ctrTV:@"6"];
}
- (IBAction)C7
{
    [self ctrTV:@"7"];
}
- (IBAction)C8
{
    [self ctrTV:@"8"];
}
- (IBAction)C9
{
    [self ctrTV:@"9"];
}
- (IBAction)C0
{
    [self ctrTV:@"0"];
}
- (IBAction)channel
{
    [self ctrTV:@"-/--"];
}
- (IBAction)TVAV
{
    [self ctrTV:@"T/AV"];
}
- (IBAction)LEFT
{
    [self ctrTV:@"LEFT"];
}
- (IBAction)DOWN
{
    [self ctrTV:@"DOWN"];
}
- (IBAction)RIGHT
{
    [self ctrTV:@"RIGHT"];
}
- (IBAction)UP
{
    [self ctrTV:@"UP"];
}
- (IBAction)OK
{
    [self ctrTV:@"OK"];
}
- (IBAction)MENU
{
    [self ctrTV:@"MENU"];
}
- (IBAction)BACK
{
    [self ctrTV:@"BACK"];
}
- (IBAction)MUTE
{
    [self ctrTV:@"MUTE"];
}

- (IBAction)VOLIncrease
{
    [self ctrTV:@"VOLJIA"];
}

- (IBAction)VOLDecrease
{
    [self ctrTV:@"VOLJIAN"];
}

- (IBAction)CHIncrease
{
    [self ctrTV:@"CHJIA"];
}

- (IBAction)CHDecrease
{
    [self ctrTV:@"CHJIAN"];
}
#pragma mark - 电器设备开关状态
- (void)ctlDevice
{
    NSString *switchId = self.equipment.switchId;
    NSString *name = self.equipment.name;
    BOOL STATE = [DeviceView deviceStateOn:self.equipment];
    BOOL OPEN = !STATE;
    [DeviceView OperatSwithcId:switchId name:name OnOff:OPEN target:self end:@selector(ctlDeviceEnd:)];
}
- (void)ctlDeviceEnd:(NSDictionary *)dic
{
    NSLog(@"%@", dic);
    
    NSNumber *result = [dic objectForKey:@"result"];
    NSString *resultTip = @"";
    if (result.integerValue == 0) {   
        BOOL STATE = [DeviceView deviceStateOn:self.equipment ];
        BOOL OPEN = !STATE;
        //操作成功
        resultTip = [OperationTip operatSuccess:YES type:ErrorTypeServerReturnFail];
        [DeviceView setDeivceOnOff:self.equipment button:OnOff State:OPEN];
        GKBMainController *main = [GKBMainController mainCtr];
        [main updateStateByNetTarget:self action:@selector(updateState:)];
    }
    else
    {
        //操作失败！原因：
        resultTip = [OperationTip operatSuccess:NO type:ErrorTypeServerReturnFail];
    }
    [iToast showTip:resultTip gravity:iToastGravityBottom];
}
- (void)updateState:(NSArray*)states
{
    [GKBMainController updateEqu:self.equipment button:OnOff states:states];
}
#pragma mark - 控制TV
- (void)ctrTV:(NSString *)ORDER
{
    NSString *order = ORDER;
    NSString *orderName = NSLocalizedString(order, nil);
    NSString *operat = NSLocalizedString(@"operat:", nil);
    NSString *startTip = [operat stringByAppendingString:orderName];
    [iToast showTip:startTip gravity:iToastGravityBottom];
    
    
    self.ctrOrder = order;
    NSString *switchId = self.equipment.switchId;
    NSString *sessionId = [LoginController1 sessionID];
    NSString *fomat = @"[{\"orderId\":\"%@\", \"switchId\":\"%@\", \"sessionId\":\"%@\"}]";
    NSString *body = [NSString stringWithFormat:fomat, order, switchId, sessionId];
    
    //操作失败！原因：
    NSString *errorTip = [OperationTip operatSuccess:NO type:ErrorTypeNetworkFail];
    [WebServices requestStartOrder:@"ctlTV" body:body target:self action:@selector(ctrTVEnd:) errorTip:errorTip];
}

- (void)ctrTVEnd:(NSDictionary *)dic
{
    NSNumber *result = [dic objectForKey:@"result"];
    if (result.integerValue == 0) {     
        //操作成功 
        NSString *tip = [OperationTip operatSuccess:YES type:ErrorTypeServerReturnFail];
        [iToast showTip:tip gravity:iToastGravityBottom];
    }
    else
    {
        //操作失败！原因：
        NSString *tip = [OperationTip operatSuccess:NO type:ErrorTypeServerReturnFail];
        [iToast showTip:tip gravity:iToastGravityBottom];
    }
}

@end
